#!/usr/bin/ruby


infile = File.new("input/Binary - OCR Original.res", "r")

margin_error = 0.5

total_corrects = 0
total_incorrects = 0

data_start = false
data_line = 0
error = 0
desired_output = ""
real_output = ""
infile.each { |i|
    # Begin data entry
    if i[0,1] == "#" then
        data_start = true
        data_line = 0
        desired_output = ""
        real_output = ""
    end
    # Process data entry
    if data_start then
        # Build string with desired output
        if data_line >= 3 and data_line <= 5 then
            desired_output = desired_output + i[0,i.length-1] + " " # Skip \n
        end
        
        # Build string with real output
        if data_line >= 6 and data_line <= 8 then
            real_output = real_output + i[0,i.length-1] + " " # Skip \n
        end
        
        # End of data entry => Check if it is correctly classfied
        if data_line == 8 then
            desired_output = desired_output.split " "
            real_output = real_output.split " "
            correct = true # Suppose correct by default
            for i in 0..desired_output.length-1
                if desired_output[i] == "1" then
                    if real_output[i].to_f <= 1 - margin_error then
                        correct = false
                    end
                else
                    if real_output[i].to_f > 0 + margin_error then
                        correct = false
                    end
                end
                error = error + (real_output[i].to_f - desired_output[i].to_f).abs
            end
            
            if correct then
                total_corrects = total_corrects + 1
            else
                total_incorrects = total_incorrects + 1
            end
            data_start = false
        end
        
        data_line = data_line + 1
    end
}
puts "-------------------------------------------------------"
puts "* Network with binary representation of letters       *"
puts "-------------------------------------------------------"
correct_percent = 100 * total_corrects.to_f / (total_corrects + total_incorrects)
correct_percent = (100 * correct_percent).round.to_f / 100
puts "Correct classifications:\t" + total_corrects.to_s + " (" + correct_percent.to_s + "%)"
incorrect_percent = 100 * total_incorrects.to_f / (total_corrects + total_incorrects)
incorrect_percent = (100 * incorrect_percent).round.to_f / 100
puts "Incorrect classifications:\t" + total_incorrects.to_s + " (" + incorrect_percent.to_s + "%)"
mean_error = error / (26 * (total_corrects + total_incorrects))
mean_error = (100 * mean_error).round.to_f / 100
puts "Mean error:\t\t\t" + mean_error.to_s
puts "-------------------------------------------------------"


infile = File.new("input/Numeric - OCR Original.res", "r")

margin_error = 0.02

total_corrects = 0
total_incorrects = 0

data_start = false
data_line = 0
desired_output = -1
real_output = -1
error = 0
infile.each { |i|
    # Begin data entry
    if i[0,1] == "#" then
        data_start = true
        data_line = 0
        desired_output = -1
        real_output = -1
    end
    # Process data entry
    if data_start then
        # Build string with desired output
        if data_line == 3 then
            desired_output = i.to_f
        end
        
        # Build string with real output
        if data_line == 4 then
            real_output = i.to_f
        end
        
        # End of data entry => Check if it is correctly classfied
        if data_line == 4 then
            
            if real_output <= desired_output + margin_error and real_output >= desired_output - margin_error then
                total_corrects = total_corrects + 1
            else
                total_incorrects = total_incorrects + 1
            end
            error = error + (real_output - desired_output).abs

            data_start = false
        end
        
        data_line = data_line + 1
    end
}
puts "-------------------------------------------------------"
puts "* Network with numeric representation of letters      *"
puts "-------------------------------------------------------"
correct_percent = 100 * total_corrects.to_f / (total_corrects + total_incorrects)
correct_percent = (100 * correct_percent).round.to_f / 100
puts "Correct classifications:\t" + total_corrects.to_s + " (" + correct_percent.to_s + "%)"
incorrect_percent = 100 * total_incorrects.to_f / (total_corrects + total_incorrects)
incorrect_percent = (100 * incorrect_percent).round.to_f / 100
puts "Incorrect classifications:\t" + total_incorrects.to_s + " (" + incorrect_percent.to_s + "%)"
mean_error = error / (total_corrects + total_incorrects)
mean_error = (100 * mean_error).round.to_f / 100
puts "Mean error:\t\t\t" + mean_error.to_s
puts "-------------------------------------------------------"




